package com.wanxi.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.TreeMap;

public class AreaService {
    private int[] array1 = new int[1999999 - 1300000 + 1];
    private String[] array2 = new String[450];

    public AreaService() {
        dealDate();//new一个对象，构造方法只执行一次，其内部的代码也只执行一次。
    }


    private void dealDate() {

        BufferedInputStream bufferedInputStream = null;
        try {
            File file = new File("E:\\chenming\\step-two\\step02-20230320\\area.txt");
            FileInputStream fileInputStream = new FileInputStream(file);
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            byte[] buffer = new byte[(int) file.length()];
            int len = bufferedInputStream.read(buffer);
            String s = new String(buffer, 0, len);
//                System.out.println(s);
            String[] areaAll = s.split("\n");
//array2[0]="ss";
            for (int i = 0; i < areaAll.length; i++) {
                String[] areaOne = areaAll[i].split("\t");
                int index1 = Integer.parseInt(areaOne[0]) - 1300000;
                int index2 = Integer.parseInt(areaOne[1]);
                array1[index1] = index2;
                String temp = areaOne[2];
                array2[index2] = temp;
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedInputStream != null)
                    bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }


    /**
     * @param mobile 手机号
     * @return 返回的地区，如果未查到，返回null
     */
    public String getArea(String mobile) {
        //14587810900, 1458781	3	四川.成都


        String areaBack = null;


        int seven = Integer.parseInt(mobile.substring(0, 7));
        int index1 = seven - 1300000;
        if (index1 < array1.length) {
            int index2 = array1[index1];
            areaBack = array2[index2];
//                System.out.println(areaBack);
        } else if (index1 >= array1.length) {
            areaBack = null;
        }


        return areaBack;
    }
}
